package worker;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import com.mongodb.DBCollection;
import com.mongodb.DBCursor;

import data.Data;

public class SetDataWorker extends TimerTask{
//	public SetDataWorker()
//	{
//		
//		System.out.println("aaa");
//	}
	public void run() {
		DBCollection coll;
		try {
			coll = Data.data("domaincraw");
			DBCursor lobj = coll.find();
			setUp();
			ArrayList<String> lastPing = new ArrayList<String>();
			while(lobj.hasNext()){
				lastPing.add(lobj.next().get("url").toString());
			}
			
			for (int i = 0; i < lastPing.size(); i++) {
				createProducerAndSendAMessage(lastPing.get(i));
				try {
					Thread.currentThread().sleep(10000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			connection.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    }
	public static void main(String[] args) throws JMSException, IOException {

		Timer timer = new Timer();
		SetDataWorker setDataWorker = new SetDataWorker();

	    // Schedule to run after every 3 second(3000 millisecond)

	    timer.scheduleAtFixedRate( setDataWorker, 0,1000*60*60);
	}
	
	private static ActiveMQConnectionFactory connectionFactory;
    private static Connection connection=null;
    private static Session session;
    private static Destination destination;
    private static boolean transacted = false;
	private static void setUp() throws JMSException, IOException {
		 File f = new File("configfeed.properties");
		 Properties pro = new Properties();
		 FileInputStream in = new FileInputStream(f);
		 pro.load(in);
		 System.out.println(pro.getProperty("server"));
         connectionFactory = new ActiveMQConnectionFactory(
                 ActiveMQConnection.DEFAULT_USER,
                 ActiveMQConnection.DEFAULT_PASSWORD,
                 "failover:tcp://67.205.109.33:61616");
         
    	 connection = connectionFactory.createConnection();
    	 connection.start();

         session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
         
         destination = session.createQueue(pro.getProperty("queue"));
     }
 
     private static void createProducerAndSendAMessage(String str) throws JMSException {
   	  
         MessageProducer producer = session.createProducer(destination);
         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
         TextMessage message = session.createTextMessage(str);
         System.out.println("Sending message: " + message.getText());
         producer.send(message);
//         connection.close();
     }
}
